package com.sanji.sjmallmodule.phoneshell;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.sanji.sjmallutils.Tools;
import com.sanji.sjmallwidget.MyListView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 定制订单详情
 * Created by Administrator on 2017/9/16.
 */

public class ShellOrderDetailActivity extends BaseActivity implements View.OnClickListener,NoHttpListener {

    @BindView(R.id.common_back_img)ImageView mCommonBackImg;
    @BindView(R.id.common_left_tv)TextView mCommonLeftTv;
    @BindView(R.id.common_right_img)ImageView mCommonRightImg;
    @BindView(R.id.shell_order_detail_order_status)TextView statusTv;
    @BindView(R.id.shell_order_detail_order_status_list)MyListView statusListView;
    @BindView(R.id.shell_order_dest_orderno_tv)TextView orderNoTv;
    @BindView(R.id.shell_order_dest_buy_date_tv)TextView buyDateTv;
    @BindView(R.id.shell_order_dest_paid_type_tv)TextView paidTypeTv;
    @BindView(R.id.shell_order_dest_pay_date_tv)TextView paidDateTv;
    @BindView(R.id.shell_order_dest_user_name_tv)TextView userNameTv;
    @BindView(R.id.shell_order_dest_user_phone_tv)TextView userPhoneTv;
    @BindView(R.id.shell_order_dest_user_address_tv)TextView userAddressTv;
    @BindView(R.id.shell_order_detail_kkmove_web)WebView kkmoveWebview;
    @BindView(R.id.shell_order_detail_brand_tv)TextView brandTv;
    @BindView(R.id.shell_order_detail_count_tv)TextView countTv;
    @BindView(R.id.shell_order_detail_price_value_tv)TextView priceTv;
    @BindView(R.id.shell_order_detail_status_value_tv)TextView odstatusTv;
    @BindView(R.id.shell_order_detail_user_name_tv)TextView memberNameTv;
    @BindView(R.id.shell_order_detail_user_phone_tv)TextView memberPhoneTv;


    private String orderDestUrl;
    private String orderNum;
    private String showView;
    private List<OrderRecordBean> recordList;
    private OrderRecordAdapter recordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shell_order_detail);
        ButterKnife.bind(this);

        orderNum = getIntent().getStringExtra("orderNum");
        showView = getIntent().getStringExtra("showView");
        initViews();
        getOrder();
    }

    private void initViews(){
        mCommonBackImg.setOnClickListener(this);
        mCommonLeftTv.setText("订单详情");
//        mCommonRightImg.setVisibility(View.VISIBLE);
//        mCommonRightImg.setImageResource(R.mipmap.fragment_main_noselect);
        int imgWidth = myShare.getInt(ConfigConts.ScreenWidth,800) / 3 ;
        kkmoveWebview.setLayoutParams(new FrameLayout.LayoutParams(imgWidth,imgWidth));
        WebSettings settings = kkmoveWebview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        kkmoveWebview.setWebViewClient(new WebViewClient());
        kkmoveWebview.loadUrl(showView);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.common_back_img:
                finishActivity();
                break;
        }
    }

    private void getOrder(){
        orderDestUrl = ConfigAPI.PHONE_SHELL_ORDER_DEST + orderNum;
        Request<JSONObject> destRequest = NoHttp.createJsonObjectRequest(orderDestUrl, RequestMethod.GET);
        destRequest.setCancelSign(orderDestUrl.hashCode());
        NoHttpCallBack.getInstance().add(mCtx, orderDestUrl.hashCode(), destRequest, this, true, true);
    }

    @Override
    public void onSuccessed(int what, Response response) {
        if (what == orderDestUrl.hashCode()){
            JSONObject obj = (JSONObject) response.get();
            String num = obj.optString("orderNum");
            String orderCreateTime = obj.optString("orderCreateTime");
            String kkmoveId = obj.optString("kkmoveId");
            String payTime = obj.optString("payTime");
            String status = obj.optString("status");
            String userMobile = obj.optString("userMobile");
            String address = obj.optString("address");
            String payType = obj.optString("payType");
            String memberName = obj.optString("memberName");
            String memberMobile = obj.optString("memberMobile");
            String goodsName = obj.optString("goodsName");
            String username = obj.optString("username");
            int quantity = obj.optInt("quantity");
            double price = obj.optDouble("price");
            int point = obj.optInt("point");
            double carriage = obj.optDouble("carriage");

            //订单信息
            statusTv.setText("当前订单状态："+status);
            orderNoTv.setText("订单编号："+num);
            buyDateTv.setText("下单时间："+orderCreateTime);
            paidTypeTv.setText("付款方式："+payType);
            paidDateTv.setText("付款时间："+payTime);
            userNameTv.setText("收货人　："+memberName);
            userPhoneTv.setText("收货电话："+memberMobile);
            userAddressTv.setText("收货地址："+address);

            //商品清单
            brandTv.setText("品牌型号："+goodsName);
            countTv.setText("数量："+quantity);
            priceTv.setText(Tools.divide(price) + "元");
            odstatusTv.setText(status);
            memberNameTv.setText(memberName);
            memberPhoneTv.setText(memberMobile);

            //订单状态列表
            JSONArray orderRecordes = obj.optJSONArray("orderRecordes");
            if (orderRecordes!=null && orderRecordes.length()>0){
                recordList = new ArrayList<>();
                int length = orderRecordes.length();
                JSONObject record;
                OrderRecordBean bean;
                for (int i=0;i<length;i++){
                    bean = new OrderRecordBean();
                    record = orderRecordes.optJSONObject(i);
                    bean.setId(record.optString("id"));
                    bean.setOperatorId(record.optString("operatorId"));
                    bean.setOperation(record.optString("operation"));
                    bean.setOperationTime(record.optString("operationTime"));
                    bean.setOrderNum(record.optString("orderNum"));
                    recordList.add(bean);
                }
                //adapter
                if (recordAdapter == null){
                    recordAdapter = new OrderRecordAdapter(mCtx,recordList);
                    statusListView.setAdapter(recordAdapter);
                }else{
                    recordAdapter.notifyDataSetChanged();
                }

            }
        }
    }

    @Override
    public void onFailed(int what, Response response) {

    }
}
